//
//  TTWebService.h
//  Tint
//
//  Created by Kevin Wu on 2/29/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Core/Core.h"
#import "Additions/Additions.h"

#import "TTDiskCache.h"
#import "Network/TTHTTPRequest.h"

#define TTWebServiceCacheName @"WebService"


@interface TTWebService : NSObject<TTOperationDelegate, TTDelegatesProtocol> {
    
    NSString *_address;
    NSString *_requestMethod;
    TTHTTPRequest *_request;
    NSTimeInterval _requestTimeoutInterval;
    NSDictionary *_requestHeaders;
    NSData *_requestBody;
    
    id _parsedObject;
    NSString *_cacheKey;
    NSTimeInterval _cacheTimeoutInterval;
    
    BOOL _loading;
    BOOL _cancelled;
    
    SEL _alreadyInCacheSelector;
    SEL _didStartSelector;
    SEL _didFailSelector;
    SEL _willFinishSelector;
    SEL _didFinishSelector;
    
    NSMutableDictionary *_parameters;
    
    NSMutableArray *_delegates;
}

@property(nonatomic, copy) NSString *address;
@property(nonatomic, copy) NSString *requestMethod;
@property(nonatomic, readonly) TTHTTPRequest *request;
@property(nonatomic, assign) NSTimeInterval requestTimeoutInterval;
@property(nonatomic, retain) NSDictionary *requestHeaders;
@property(nonatomic, retain) NSData *requestBody;

@property(nonatomic, retain) id parsedObject;
@property(nonatomic, copy) NSString *cacheKey;
@property(nonatomic, assign) NSTimeInterval cacheTimeoutInterval;

@property(nonatomic, readonly) BOOL loading;
@property(nonatomic, readonly) BOOL cancelled;

@property(nonatomic, assign) SEL alreadyInCacheSelector;
@property(nonatomic, assign) SEL didStartSelector;
@property(nonatomic, assign) SEL didFailSelector;
@property(nonatomic, assign) SEL willFinishSelector;
@property(nonatomic, assign) SEL didFinishSelector;


+ (id)sharedObject;
+ (BOOL)hasSharedObject;
+ (void)destroySharedObject;
+ (id)serviceByName:(NSString *)name;
+ (BOOL)hasServiceForName:(NSString *)name;
+ (void)destroyServiceByName:(NSString *)name;
+ (NSDictionary *)serviceMap;

- (void)initialize;

- (void)setParameters:(NSDictionary *)dictionary;
- (void)addParameters:(NSDictionary *)dictionary;
- (void)addParameter:(NSString *)parameter forName:(NSString *)name;
- (void)removeParameterForName:(NSString *)name;
- (NSString *)parameterForName:(NSString *)name;
- (void)clearParameters;
- (void)addPropertiesAsParameter;
- (void)addPropertiesAsParameter:(NSString *)propertyString;
- (void)addPropertiesAsParameterExcept:(NSString *)exceptPropertyString;
- (void)addPropertiesInArrayAsParameter:(NSArray *)properties exceptProperties:(NSArray *)exceptProperties;
- (NSString *)stringValueForProperty:(NSString *)name;

- (void)start; // Request immediately, no cache
- (void)startWithCacheTimeoutInterval:(NSTimeInterval)timeoutInterval; // Request depends on cache, cache
- (void)refreshWithCacheTimeoutInterval:(NSTimeInterval)timeoutInterval; // Request immediately, cache
- (void)cancel;

- (void)parse:(NSData *)data;
- (id)newObjectFromData:(NSData *)data;
- (void)saveResult:(id)result;

- (BOOL)isInCache;
- (NSDate *)updateDate;

- (void)webServiceAlreadyInCache;
- (void)webServiceDidStart;
- (void)webServiceDidFail:(NSError *)error;
- (void)webServiceWillFinish;
- (void)webServiceDidFinish;

@end



@protocol TTWebServiceDelegate <NSObject>
@optional
- (void)webServiceAlreadyInCache:(TTWebService *)service;
- (void)webServiceDidStart:(TTWebService *)service;
- (void)webServiceDidFail:(TTWebService *)service withError:(NSError *)error;
- (void)webServiceWillFinish:(TTWebService *)service;
- (void)webServiceDidFinish:(TTWebService *)service;
@end

